$(function () {
    let $doc = $(document);
    let blog = {
        init: function () {
            this.event();
            $('.comment').length?this.addMsg.init():0;
        },
        event: function () {
            let that = this;
        },
        addMsg: {
            init: function () {
                $.ajax({
                    url: "/jigsaw?t=" + (new Date()).getTime(), // 加随机数防止缓存
                    type: "get",
                    dataType: "json",
                    success: function (data) {
                        $('#text').hide();
                        $('#wait').show();
                        initGeetest({
                            gt: data.gt,
                            challenge: data.challenge,
                            offline: !data.success,
                            new_captcha: data.new_captcha,
                            product: "float", // 产品形式，包括：float，popup
                            width: "300px",
                            https: true
                        }, blog.addMsg.handler);
                    }
                });
            },
            handler: function (captchaObj) {
                captchaObj.appendTo('#captcha');
                captchaObj.onReady(function () {
                    $("#wait").hide();
                });
                $('.comment').submit(function (e) {
                    e.preventDefault();
                    let $form = $(this);
                    let data = $form.serialize();
                    let result = captchaObj.getValidate();
                    if (!result) {
                        $.tipTop(false, '请先完成验证');
                        return false;
                    }
                    $.ajax({
                        url: $form.attr('action'),
                        type: 'POST',
                        dataType: 'json',
                        data: data,
                        success: function (res) {
                            $.tipTop(res.status, res.msg);
                            if (!res.status) {
                                captchaObj.reset();
                            } else {
                                setTimeout(function () {
                                    window.location.reload()
                                }, 1000)
                            }
                        }
                    });
                });
                window.gt = captchaObj;
            }
        },

    };
    blog.init();
});